const fs  = require("fs");
const http = require("http");

const app = http.createServer((req,res)=>{
    // res.end("ok")
    if(req.url === "/index.html"){
        res.setHeader("content-type","text/html;charset=utf8");
        fs.readFile("./public/index.html",(err,data)=>{
            if(err){
                console.log("读取失败：",err);
                res.end("读取失败！")
                return
            }
            res.end(data);
        })
    }else if(req.url === "/style.css"){
        res.setHeader("content-type","text/css;charset=utf8");
        fs.readFile("./public/style.css",(err,data)=>{
            if(err){
                console.log("读取失败：",err);
                res.setHeader("content-type","text/html;charset=utf8");
                res.end("读取失败！");
                return
            }
            res.end(data);
        })
    }else if(req.url === "/images/bg.jpg"||req.url === "/images/fm.jpg"){
        res.setHeader("content-type", "image/jpg")
        fs.readFile("./public"+req.url, (err,data)=>{
            if(err){
                console.log("读取失败：",err);
                res.setHeader("content-type","text/html;charset=utf8");
                res.end("读取失败！");
                return
            }
            res.end(data)
        })
    }else{
        res.statusCode = 404;
        res.setHeader("content-type","text/html;charset=utf8");
        res.end("{msg: '文件找不到',code: 404}")
    }
})

app.listen("8003",()=>{
    console.log("服务器开启成功：http://127.0.0.1:8003");
})